package com.example.controller;

import com.example.pojo.SaleChance;
import com.example.service.ISaleChanceService;
import com.example.util.BaseController;
import com.example.util.CookieUtil;
import com.example.vo.ResultInfo;
import com.example.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/mar/salechance")
@CrossOrigin(allowCredentials = "true")
@Api(tags = "营销管理")
public class SaleChanceController extends BaseController {
    @Autowired
    private ISaleChanceService saleChanceService;


    @GetMapping("/pager")
    @ApiOperation(value = "获得营销管理分页信息")
    public PageInfo<SaleChance> displayPager(@RequestParam(defaultValue = "1")Integer pageNum,
                                             @RequestParam(defaultValue = "5")Integer pageSize,
                                             String customerName,
                                             String createMan,
                                             Integer state,
                                             Integer devResult,
                                             String chanceSource){
        return saleChanceService.electByPager(pageNum,pageSize,customerName,createMan,state,devResult,chanceSource);
    }

    /**
     * 添加营销机会
     */
    @ApiOperation(value = "添加营销机会")
    @PostMapping("/add")
    public ResultVo addSaleChance(@RequestBody SaleChance saleChance, HttpServletRequest request){
        //从Cookie中获取当前登录的用户名
        String userName= CookieUtil.getCookieValue(request,"userName");
        //设置用户名到营销机会对象
        saleChance.setCreateMan(userName);
        //调用Service层添加方法
        return saleChanceService.addSaleChance(saleChance);
    }

    /**
     * 修改营销机会
     * @return
     */
    @ApiOperation(value = "修改营销机会")
    @PutMapping("/update")
    public ResultInfo updateSaleChance(@RequestBody SaleChance saleChance){
        saleChanceService.updateById(saleChance);
        return success("营销机会数据修改成功！");
    }

    /**
     * 查询所有
     */
    @GetMapping("")
    public List<SaleChance> displaySaleChanceInfo(){
        return saleChanceService.selectAll();
    }
    /**
     * 查询回收站
     */
    @GetMapping("/del")
    @ApiOperation(value = "回收站")
    public ResultVo deleted(){
        return saleChanceService.deleted();
    }

    /**
     *回收站还原操作
     */
    @ApiOperation(value = "回收站还原")
    @PutMapping("/hy/{id}")
    public ResultVo restore(@PathVariable("id") Integer id){
        SaleChance saleChance = new SaleChance().setId(id).setDeleted(0);
        if (saleChanceService.updateById(saleChance)){
            return ResultVo.ok("还原成功！");
        }
        return ResultVo.error("还原失败！");
    }


    //@PutMapping 修改
    //@PostMapping 新增
    //@GetMapping 查询
    //@DeleteMapping 删除
    @ApiOperation(value = "删除营销机会")
    @DeleteMapping("/{id}")
    public ResultVo del(@PathVariable("id") Integer id){
        SaleChance saleChance = new SaleChance().setId(id).setIsValid(0);
        if(saleChanceService.removeById(saleChance)){
            return ResultVo.ok("修改成功。");
        }
        return ResultVo.error("修改失败!");
    }
}
